package com.oracle.lintener;

import com.alibaba.dubbo.rpc.RpcContext;
import com.oracle.config.service.api.SystemConfigApi;
import com.oracle.properties.PropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

@Component
public class InitLintener implements ServletContextAware,ApplicationListener<ContextRefreshedEvent> {

    //定义servlet上下文对象
    private ServletContext servletContext;
    @Autowired(required = false)
    private SystemConfigApi systemConfigApi;
    @Autowired
    private PropertiesUtils propertiesUtils;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (servletContext!=null && event.getApplicationContext().getParent()==null){
            //获取rpc上下文对象
            RpcContext rpcContext = RpcContext.getContext();
            //存放rpc的隐士参数项(注意有一些key是不能写的，如：group、version、token....)
            //并且这个密钥匙需要定期更换
            rpcContext.setAttachment("secret","SDUENK7IASJD9#DIASD$JDA998");
            Integer fileCountMaxValue = this.systemConfigApi.getFileCountMaxValue().getData();
            this.propertiesUtils.setValue("failcountmax",fileCountMaxValue+"");
            System.out.println("failcountmax----->"+this.propertiesUtils.getValue("failcountmax"));

            rpcContext.setAttachment("secret","SDUENK7IASJD9#DIASD$JDA998");
            Integer loopMaxValue = this.systemConfigApi.getLoopMaxValue().getData();
            this.propertiesUtils.setValue("loopmax",loopMaxValue+"");
            System.out.println("loopmax----->"+this.propertiesUtils.getValue("loopmax"));

            System.out.println("====获取系统配置信息完成====");
            servletContext.setAttribute("root",servletContext.getContextPath());
            servletContext.setAttribute("js",servletContext.getContextPath()+"/resources/js");
            servletContext.setAttribute("image",servletContext.getContextPath()+"/resources/image");
            servletContext.setAttribute("css",servletContext.getContextPath()+"/resources/css");
            servletContext.setAttribute("kindeditor",servletContext.getContextPath()+"/resources/kindeditor");
            System.out.println("静态资源加载-----------");
        }
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        //初始化servletContext
        this.servletContext = servletContext;
    }

}
